package com.shujia;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

//将每一个map任务产生的结果做总的聚合
public class Reduce {
    public static void main(String[] args) throws IOException {
        BufferedReader br = null;
        //定义个map集合
        HashMap<String, Integer> map = new HashMap<>();

        //将data/parts目录封装成一个File对象
        File file = new File("data/parts");
        File[] files = file.listFiles();
        long start = System.currentTimeMillis();
        if (files != null) {
            for (File file1 : files) {
                //创建一个字符缓冲输入流对象
                br = new BufferedReader(new FileReader(file1.getAbsolutePath()));
                String line = null;
                while ((line = br.readLine()) != null) {
                    String clazz = line.split(":")[0];
                    String num = line.split(":")[1];
                    if (!map.containsKey(clazz)) {
                        map.put(clazz, new Integer(num));
                    } else {
                        map.put(clazz, map.get(clazz) + new Integer(num));
                    }
                }
            }
        }
        if (br != null) {
            br.close();
        }
        //将集合的结果写入到文件中
        BufferedWriter bw = new BufferedWriter(new FileWriter("data/part/part-r-00000"));
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            String clazz = entry.getKey();
            Integer numbers = entry.getValue();
            bw.write(clazz + ":" + numbers);
            bw.newLine();
            bw.flush();
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start) + "毫秒");

        bw.close();

    }
}
